package com.ruoyi.web.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.business.Visit;
import com.ruoyi.business.enums.CitySplit;
import com.ruoyi.business.qo.VisitQO;
import com.ruoyi.business.service.VisitService;
import com.ruoyi.business.vo.VisitVO;
import com.ruoyi.common.common.CommonEnum;
import com.ruoyi.common.common.GlobalValue;
import com.ruoyi.common.common.ResultBody;
import com.ruoyi.common.utils.IpUtil;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.lionsoul.ip2region.xdb.Searcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @title: CustomerController
 * @copyright: copyright (c) 2021
 * @author: jiangguanjun
 * @date: 2023/1/29 14:56
 * @firstReview:
 * @lastReview:
 * @desc: 访问相关
 */
@Api(value = "VisitController", tags = "访问相关")
@RestController
@Slf4j
@RequestMapping("/visit")
public class VisitController {
    private static final Integer VISIT_OUT_TIME = 24;
    @Autowired
    private VisitService visitService;

    @Autowired
    private RedisTemplate redisTemplate;


    @Value("${city.path}")
    private String path  ;

    @CrossOrigin
    @PostMapping("/add")
    @ApiOperation(value = "添加网站浏览记录，在用户进入网站时候调用,需要填写cId，该值写死，问后台要")
    public ResultBody add(HttpServletRequest request, @RequestBody Visit visit){
        log.info("visit:::{}",visit);

        String ip = IpUtil.getIpAddrs(request) ;
        log.info("ip:::::::::{}",ip);

        String key = GlobalValue.getVisttIpKey(ip) ;
        Object value = redisTemplate.opsForValue().get(key) ;
        if(value!=null){
            log.info("{}小时内重复浏览，直接返回缓存数据，不需要添加数据库",VISIT_OUT_TIME);
            return ResultBody.success(value.toString()) ;
        }

        log.info(path);
        Searcher searcher = null;
        try {
            searcher = Searcher.newWithFileOnly(path);
        } catch (IOException e) {
            log.error("failed to create searcher with : {}", path, e);
            return null;
        }
        String city = IpUtil.getCityDetailByIp(ip,searcher,",", CitySplit.province.getIndex(),CitySplit.city.getIndex()) ;
        visit.setvAddress(city);
        visit.setvIp(ip);
        int  res = visitService.save(visit) ;
        // 设置城市缓存的失效时间为24小时
        if(res>0){

            redisTemplate.opsForValue().set(key,city,VISIT_OUT_TIME, TimeUnit.HOURS);
            return ResultBody.success(city) ;
        }

        return ResultBody.error(CommonEnum.VISIT_ADD_ERROR) ;
    }

    @CrossOrigin
    @PostMapping("/list")
    @ApiOperation(value = "网站浏览记录列表  页面：采购-浏览记录")
    public ResultBody<IPage<VisitVO>> list(@RequestBody VisitQO visitQO){
        log.info("传入参数：{}",visitQO);
        if(StringUtils.isNotBlank(visitQO.getStartTime())){
            visitQO.setStartTime(visitQO.getStartTime()+" 00:00:00");
        }

        if(StringUtils.isNotBlank(visitQO.getEndTime())){
            visitQO.setEndTime(visitQO.getEndTime()+" 23:59:59");
        }
        return visitService.queryPage(visitQO) ;
    }



}
